﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZilLion.Service.Core.DapperWrapper;
using ZilLion.Service.Core.Snowflake;
using ZilLion.Service.ZilLionOss.Domain.Entitys;
using ZilLion.Service.ZilLionOss.Domain.Repositorys.Interface;
using ZilLion.Service.ZilLionOss.Domain.Services.Interface;

namespace ZilLion.Service.ZilLionOss.Domain.Services
{
    public class OssCompanyService : IOssCompanyService
    {
        private readonly IOssCompanyInfoReponsitory _ossCompanyInfoReponsitory;
        private readonly IOssCompanyMoudleReponsitory _ossCompanyMoudleReponsitory;
        private readonly IOssCompanyRouteReponsitory _ossCompanyRouteReponsitory;
        private readonly IOssRouteRepositrory _ossRouteRepositrory;
        private readonly IOssUserInfoRepository _ossUserInfoRepository;
        private readonly IOssUserPwdRepository _ossUserPwdRepository;
        private readonly IOssUserRoleRepository _ossUserRoleRepository;

        public OssCompanyService(IOssCompanyMoudleReponsitory ossCompanyMoudleReponsitory,
            IOssCompanyInfoReponsitory ossCompanyInfoReponsitory, IOssUserInfoRepository ossUserInfoRepository,
            IOssUserPwdRepository ossUserPwdRepository, IOssUserRoleRepository ossUserRoleRepository,
            IOssCompanyRouteReponsitory ossCompanyRouteReponsitory, IOssRouteRepositrory ossRouteRepositrory)
        {
            _ossCompanyMoudleReponsitory = ossCompanyMoudleReponsitory;
            _ossCompanyInfoReponsitory = ossCompanyInfoReponsitory;
            _ossUserInfoRepository = ossUserInfoRepository;
            _ossUserPwdRepository = ossUserPwdRepository;
            _ossUserRoleRepository = ossUserRoleRepository;
            _ossCompanyRouteReponsitory = ossCompanyRouteReponsitory;
            _ossRouteRepositrory = ossRouteRepositrory;
        }

        public IList<OssCompanyDto> GetAllCompanys()
        {
            var companyList = _ossCompanyInfoReponsitory.GetAllCompany();
            var companyMoudleList = _ossCompanyMoudleReponsitory.GetAllCompanyMoudle();

            var result = new List<OssCompanyDto>();
            foreach (var company in companyList)
            {
                var dto = company.AutoConvert<OssCompanyDto>();
                var first = companyMoudleList.Where(x => x.ZlcompanyKey == company.ZlcompanyKey).ToList();
                dto.Moudles = first;
                result.Add(dto);
            }
            return result;
        }


        public IList<OssRouteInfoEntity> GetCompanyRoutes(string companykey)
        {
            return _ossRouteRepositrory.GetRouteByCompanykey(companykey);
        }

        public void SetCompanyRoutes(IList<OssCompanyRouteEntity> routes)
        {
            using (var unit = new UnitOfWork())
            {
                foreach (var route in routes)
                {
                    _ossCompanyRouteReponsitory.RemoveRoute(route, unit);
                    _ossCompanyRouteReponsitory.AddRoute(route, unit);
                }
                unit.Complete();
            }
        }

        public OssCompanyDto GetCompany(string companykey)
        {
            var company = _ossCompanyInfoReponsitory.GetCompanyByKey(companykey);
            var companyMoudle = _ossCompanyMoudleReponsitory.GetCompanyMoudleByCompanyKey(companykey);
            var dto = company == null ? new OssCompanyDto() : company.AutoConvert<OssCompanyDto>();
            dto.Moudles = companyMoudle;
            return dto;
        }

        public OssCompanyDto SaveCompany(OssCompanyDto company)
        {
            var info = _ossCompanyInfoReponsitory.GetCompanyByKey(company.ZlcompanyKey);
            if (info != null)
                using (var unit = new UnitOfWork())

                {
                    company.UpdateTime = DateTime.Now.GetNowChinaTime();
                    _ossCompanyInfoReponsitory.EditCompany(company, unit);
                    _ossCompanyMoudleReponsitory.RemoveMoudleByCompanyKey(company.ZlcompanyKey, unit);
                    foreach (var moudle in company.Moudles)
                    {
                        moudle.ZlcompanyKey = company.ZlcompanyKey;
                        moudle.UpdateTime = DateTime.Now.GetNowChinaTime();
                        _ossCompanyMoudleReponsitory.AddMoudle(moudle, unit);
                    }

                    unit.Complete();
                }
            else
                using (var unit = new UnitOfWork())
                {
                    company.UpdateTime = DateTime.Now.GetNowChinaTime();
                    _ossCompanyInfoReponsitory.AddCompany(company, unit);
                    _ossCompanyMoudleReponsitory.RemoveMoudleByCompanyKey(company.ZlcompanyKey, unit);
                    foreach (var moudle in company.Moudles)
                    {
                        moudle.ZlcompanyKey = company.ZlcompanyKey;
                        moudle.UpdateTime = DateTime.Now.GetNowChinaTime();
                        _ossCompanyMoudleReponsitory.AddMoudle(moudle, unit);
                    }

                    #region 产生新租户的默认admin

                    var newuserid = SnowflakeIdCreater.Instance().GetNextId();
                    _ossUserInfoRepository.SaveUserInfo(new OssUserInfoEntity
                    {
                        ZlCompanyKey = company.ZlcompanyKey,
                        UserId = newuserid,
                        UserName = company.ZlcompanyKey + "." + "admin",
                        UserNickName = company.CompanyName + "_" + "系统管理员",
                        UserAvatar = "",
                        UserPhoneNo = 0
                    }, unit);
                    _ossUserPwdRepository.SavePwd(
                        new OssUserPwdEntity
                        {
                            Userpwd = "1047219cb50e359761667f4e2ce76b9a",
                            Userid = newuserid,
                            Username = company.ZlcompanyKey + "." + "admin"
                        }, unit);
                    _ossUserRoleRepository.AddOssUserRoles(new OssUserRoleEntity {UserId = newuserid, RoleId = -1},
                        unit);

                    #endregion

                    unit.Complete();
                }
            return GetCompany(company.ZlcompanyKey);
        }

        public void RemoveCompany(string companykey)
        {
            using (var unit = new UnitOfWork())
            {
                _ossCompanyInfoReponsitory.RemoveCompany(companykey, unit);
                _ossCompanyMoudleReponsitory.RemoveMoudleByCompanyKey(companykey, unit);
                unit.Complete();
            }
        }
    }
}